<template>
  <el-header class="layout-header">
    <img alt="华商知了" title="华商知了" class="logo" src="../assets/images/home/logo.png" />

    <div class="custom-menu">
      <!-- 在小屏幕上显示折叠菜单 -->
      <div v-if="state.isMobile" class="mobile-menu">
        <div class="btn" @click="onCooperate">
          <img src="../assets/images/cooperate/ICON_PARTNER@2x.png" />
          <span>伙伴登录</span>
        </div>
        <div class="mobile-menu-icon" @click="toggleMobileMenu">
          <el-icon v-if="state.isMobileMenuOpen"><CloseBold /></el-icon>
          <img v-else src="../assets/images/导航.svg" style="width: 2rem; height: 2rem" />
          <!-- <el-icon v-else><Menu /></el-icon> -->
        </div>
        <Transition name="slide-fade">
          <div class="collapsible-menu" v-if="state.isMobileMenuOpen">
            <div
              v-for="item in state.menuItems"
              :class="{ 'active-menu': item.id === state.activeMenu }"
              class="menu-item"
              :key="item.id"
              :title="item.title"
              @click="navigateTo(item)"
            >
              {{ item.title }}
            </div>
          </div>
        </Transition>
      </div>

      <!-- 在大屏幕上显示菜单 -->
      <div v-else class="desktop-menu">
        <ul>
          <li
            v-for="item in state.menuItems"
            :class="{ 'active-menu': item.id === state.activeMenu }"
            :key="item.id"
            :index="item.id"
            @click="navigateTo(item)"
          >
            {{ item.title }}
          </li>
        </ul>
        <div class="btn" @click="onCooperate">
          <img src="../assets/images/cooperate/ICON_PARTNER@2x.png" />
          <span>伙伴登录</span>
        </div>
      </div>
    </div>

    <ConstructDialog ref="ConstructDialogRef"></ConstructDialog>
  </el-header>
</template>

<script setup lang="ts" name="LayoutHeader">
import { defineAsyncComponent, onMounted, reactive, ref, watch } from 'vue'
import { useWindowSize } from '@vueuse/core'
import { useRouter } from 'vue-router'
// 引入组件
const CooperateDialog = defineAsyncComponent(() => import('../components/CooperateDialog.vue'))
const ConstructDialog = defineAsyncComponent(() => import('../components/ConstructDialog.vue'))
const CooperateDialogRef = ref()
const ConstructDialogRef = ref()
const router = useRouter()
const { width } = useWindowSize()

const state = reactive({
  activeMenu: '1', // 当前选中的菜单项
  isMobile: false,
  isMobileMenuOpen: false, // 是否展开手机端菜单
  menuItems: [
    { id: '1', title: '首页', url: '/home' },
    { id: '2', title: '公司介绍', url: '/about' },
    { id: '3', title: '合作加盟', url: '/cooperate' }
  ]
})

watch(
  () => width.value,
  (val) => {
    if (val < 1024) {
      state.isMobile = true
    } else {
      state.isMobile = false
    }
    console.log(val)
  },
  { immediate: true }
)

onMounted(() => {
  let activeMenu = localStorage.getItem('activeMenu')
  if (activeMenu) {
    state.activeMenu = activeMenu
  } else {
    state.activeMenu = '1'
  }
})

// 展开手机端菜单
const toggleMobileMenu = () => {
  state.isMobileMenuOpen = !state.isMobileMenuOpen
}

// 路由跳转
const navigateTo = (item: any) => {
  state.activeMenu = item.id // 当前选中的菜单
  router.push({ path: item.url })
  state.isMobileMenuOpen = false
  localStorage.setItem('activeMenu', item.id)
}

// 合作加盟
const onCooperate = () => {
  ConstructDialogRef.value.openDialog()
}
</script>

<style lang="scss" scoped>
.layout-header {
  z-index: 9;
  position: absolute;
  top: 0;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  width: 100%;
  height: 70px;
  background-color: rgba(0, 0, 0, 0.5);
  padding-left: 3rem;
  padding-right: 3rem;

  .logo {
    height: 100%;
    width: 200px;
  }
  .custom-menu {
    color: #fff;
    cursor: pointer;
    display: flex;
    flex-direction: row;
    align-content: center;
    justify-content: center;
    align-items: center;

    .mobile-menu {
      display: flex;
      flex-direction: row;
      align-items: center;
      .btn {
        margin-right: 1rem;
        cursor: pointer;
        width: 8rem;
        height: 2.6rem;
        background-color: #b72320;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        color: #fff;
        font-size: 1rem;
        border-radius: 2px;

        img {
          display: block;
          width: 1.5rem;
          margin-right: 0.5rem;
        }
      }
      .mobile-menu-icon {
        width: 2rem;
        height: 2rem;
        font-size: 2rem;
      }
      .slide-fade-enter-active {
        transition: all 0.3s ease-in;
      }

      .slide-fade-leave-active {
        transition: all 0.3s ease-out;
      }

      .slide-fade-enter-from,
      .slide-fade-leave-to {
        transform: translateY(100%);
        opacity: 0;
      }
    }
    .collapsible-menu {
      position: absolute;
      top: 70px;
      right: 0;
      right: 0;
      height: 10rem;
      width: 35vw;
      background-color: #1a1a1a;
      .active-menu {
        color: #fff !important;
        font-weight: 500;
      }

      .menu-item {
        color: #ffffffab;
        text-align: center;
        height: 3rem;
        line-height: 3rem;
        font-size: 1.3rem;
      }
    }

    .desktop-menu {
      display: flex;
      flex-direction: row;
      align-items: center;
      color: rgba(255, 255, 255, 0.6);

      .active-menu {
        color: #fff;
        font-weight: 500;
      }

      ul {
        display: flex;
        list-style: none;
        padding: 0;
        margin: 0;
      }

      li {
        font-size: 1.2rem;
        margin-right: 4rem;
        &:hover {
          color: #fff;
        }
      }

      li:last-child {
        margin-right: 0;
      }

      .btn {
        margin-left: 5rem;
        margin-right: 2rem;
        cursor: pointer;
        width: 12rem;
        height: 3rem;
        background-color: #b72320;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        color: #fff;
        font-size: 1.3rem;
        border-radius: 2px;

        img {
          display: block;
          width: 2rem;
          margin-right: 1rem;
        }
      }
    }
  }
}

@media screen and (max-width: 1024px) {
  .layout-header {
    padding: 0 0.5rem;
    .logo {
      width: 10rem;
      height: 4.5rem;
    }
    .custom-menu {
      .desktop-menu li {
        font-size: 1rem;
        margin-right: 0.8rem;
      }
    }
  }
}
</style>
